//
//  WmcJoinTeamSearchCell.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/15.
//

import UIKit

class WmcJoinTeamSearchCell: WmcBaseTableCell {
    let lineSpace = 16
    let intriSpace = 15
    let mwidth = (Wmc_screenWidth - 80 - 15 * 4) / 5.0

    var model: WmcMediaTeamInfoModel?

    /// 加入回调
    var joinBlock: (() -> Void)?

    override func makeUI() {
        contentView.addSubview(bgView)
        bgView.addSubview(teamNameLabel)
        bgView.addSubview(teamIdLabel)
        bgView.addSubview(collectionView)
        bgView.addSubview(joinBtn)

        bgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }

        teamNameLabel.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(16)
        }

        teamIdLabel.snp.makeConstraints { make in
            make.top.equalTo(teamNameLabel.snp.bottom).offset(4)
            make.left.equalTo(16)
        }

        collectionView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(teamIdLabel.snp.bottom).offset(16)
            make.right.equalTo(-16)
            make.height.equalTo(self.getHeightOfCollectionView(count: 7))
        }

        joinBtn.snp.makeConstraints { make in
            make.top.equalTo(collectionView.snp.bottom).offset(16)
            make.centerX.equalToSuperview()
            make.width.equalTo(164)
            make.height.equalTo(40)
            make.bottom.equalTo(-16)
        }

        joinBtn.rx.controlEvent(.touchUpInside).bind { [weak self] _ in
            guard let weakSelf = self, let b = weakSelf.joinBlock else { return }
            b()
        }.disposed(by: rx.disposeBag)
    }

    func configure(model: WmcMediaTeamInfoModel) {
        self.model = model
        teamNameLabel.text = model.teamName
        teamIdLabel.text = "团队号:\(model.teamOpenId)"
        collectionView.reloadData()

        collectionView.snp.updateConstraints { make in
            make.height.equalTo(self.getHeightOfCollectionView(count: model.memberList.count))
        }
    }

    lazy var bgView: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 10
        view.layer.borderWidth = 1
        view.clipsToBounds = true
        view.layer.borderColor = UIColor.wmc_grayH19_F5F5F5.cgColor
        return view
    }()

    lazy var teamNameLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH2_323233
        view.font = 14.yp.uiFont(weight: .semibold)
        view.text = "万科龙湖世纪地产"
        return view
    }()

    lazy var teamIdLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.wmc_grayH5_606066
        view.font = 14.yp.uiFont
        view.text = "万科龙湖世纪地产"
        return view
    }()

    lazy var collectionView: UICollectionView = {
        let view = UICollectionView(frame: .zero, collectionViewLayout: self.layout)
        view.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        view.delegate = self
        view.dataSource = self
        view.register(SearchTeamMemberCell.self, forCellWithReuseIdentifier: "SearchTeamMemberCell")
        view.backgroundColor = UIColor.wmc_white_FFFFFF
        return view
    }()

    lazy var layout: UICollectionViewFlowLayout = {
        let lay = UICollectionViewFlowLayout()
        lay.itemSize = CGSize(width: mwidth, height: mwidth)
        lay.minimumLineSpacing = 16
        lay.minimumInteritemSpacing = 15
        return lay
    }()

    lazy var joinBtn: WmcNormalButton = {
        let view = WmcNormalButton()
        view.backgroundColor = UIColor.wmc_mainTheme
        view.font = 16.yp.uiFont(weight: .semibold)
        view.textColor = UIColor.wmc_white_FFFFFF
        view.title = "加入"
        view.textAlignment = .center
        view.layer.cornerRadius = 4
        view.clipsToBounds = true
        return view
    }()

    /// 计算collectionview 高度
    /// - Parameter count: 成员个数
    /// - Returns: description
    func getHeightOfCollectionView(count: Int) -> CGFloat {
        var row = count / 5
        let m = count % 5
        if m > 0 {
            row = row + 1
        }
        if row > 2 {
            row = 2
        }
        return CGFloat(row) * mwidth + CGFloat((row - 1) * lineSpace)
    }
}

extension WmcJoinTeamSearchCell: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in _: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
        if let count = model?.memberList.count {
            return count
        }
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchTeamMemberCell", for: indexPath) as! SearchTeamMemberCell
        if let memeber = model?.memberList[indexPath.row] {
            cell.configure(model: memeber)
        }
        return cell
    }
}

class SearchTeamMemberCell: WmcBaseCollectionCell {
    override func makeUI() {
        contentView.addSubview(avatarView)
        avatarView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
        avatarView.nameLabel.text = "张三"
    }

    func configure(model: WmcMediaTeamInfoModel.WmcMediaTeamMemberModel) {
        let memb = WmcTeamMember(name: model.remark, img: model.avatar)
        avatarView.member = memb
    }

    lazy var avatarView: WmcMemberAvatarView = {
        let view = WmcMemberAvatarView()
        view.nameLabel.backgroundColor = UIColor.wmc_mainTheme.withAlphaComponent(0.5)
        return view
    }()
}
